package com.student;


import com.student.common.Subject;
import com.student.intf.IStudentReader;
import com.student.intf.IStudentVisitor;
import java.io.*;
import java.util.List;
import java.util.Map;

public class Main {

    private StudentManager studentManager = null;

    public static void main(String[] args) throws IOException {
        Main main = new Main();
        main.startup("D:\\JAVA\\练习\\students.txt");
        main.printAll();
        Student studentById = main.getStudentById("222021321072036");
        List<Student> byScore = main.findByScore("数学", 85, 90);
        List<Student> byAvgScore = main.findByScore(90, 92);
        List<Student> sort = main.sortByTotal();
        main.saveAs("D:\\JAVA\\studentsFile.txt");
        //main.execute();
        main.shutdown();
    }



    public void startup(String path) throws IOException {
        List<Student> students = this.loadData(path);
        this.studentManager = this.createManager(students);
    }

    public void shutdown() {
        System.out.println("运行结束！");
    }



    public List<Student> loadData(String path) throws IOException {
        IStudentReader reader = StudentReaderFactory.create(path);
        IStudentReader math = StudentReaderFactory.create(Subject.MATH_FILE_PATH);
        IStudentReader chinese = StudentReaderFactory.create(Subject.CHINESE_FILE_PATH);
        IStudentReader english = StudentReaderFactory.create(Subject.ENGLISH_FILE_PATH);
        List<Student> studentList = reader.read();
        Map<String, String> mathMap = math.readNetFile();
        Map<String, String> chineseMap = chinese.readNetFile();
        Map<String, String> englishMap = english.readNetFile();
        studentList.stream().forEach(student -> {
            student.setMathScore(Integer.valueOf(mathMap.get(student.getId())));
            student.setChineseScore(Integer.valueOf(chineseMap.get(student.getId())));
            student.setEnglishScore(Integer.valueOf(englishMap.get(student.getId())));
        });
        return studentList;
    }

    public StudentManager createManager(List<Student> students) {
        StudentManager manager = new StudentManager();
        students.forEach(s -> {
            manager.addStudent(s);
        });
        return manager;
    }

    /**读取成绩**/

    public Map<String, String> readScore(String uri) throws IOException {
        IStudentReader reader = StudentReaderFactory.create(uri);
        Map<String, String> scoreMap = reader.readNetFile();
        return scoreMap;
    }

    public void printAll() {
        this.studentManager.printAll();
    }

    public Student getStudentById(String id) {
        return this.studentManager.getStudentById(id);
    }

    public List<Student> findByScore(String classType, int min, int max) {
        return this.studentManager.findByScore(classType, min, max);
    }

    public List<Student> findByScore(int min, int max) {
        return this.studentManager.findByScore(min, max);
    }

    public List<Student> sortByTotal() {
        return this.studentManager.sortByTotal();
    }

    public void saveAs(String path) throws IOException {
        this.studentManager.saveAs(path);
    }


}